package com.frostmourne.mxsattributeinventory.commands;

import com.frostmourne.mxsattributeinventory.MXSAttributeInventory;
import com.frostmourne.mxsattributeinventory.utils.FileUtils;
import com.frostmourne.mxsattributeinventory.utils.MsgUtils;
import com.gitee.mxstars.builder.InventoryBuilder;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainCommand implements TabExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.hasPermission("server.admin")){
            return true;
        }
        if (args.length == 0){
            MsgUtils.sendMsg(sender,"&f&l属性背包 &fMXSAttributeInventory");
            MsgUtils.sendMsg(sender,"&7&oby Frostmourne and baka_gua");
            MsgUtils.sendMsg(sender,"&f&m一一一一一一一一一一一");
            MsgUtils.sendMsg(sender,"&f/mxsai open <背包> &6打开属性背包");
            MsgUtils.sendMsg(sender,"&f/mxsai reload &6重载属性背包");
        }else{
            if (args[0].equalsIgnoreCase("reload")){
                MXSAttributeInventory.getPlugin().load();
                MsgUtils.sendMsg(sender,"&f插件配置重载完成");
                return true;
            }
            if (args[0].equalsIgnoreCase("open")){
                if (!(sender instanceof Player)){
                    MsgUtils.sendMsg(sender,"&c控制台无法使用该指令");
                    return true;
                }
                if (args.length == 1){
                    MsgUtils.sendMsg(sender,"&c缺少参数:&e<背包>");
                    return true;
                }else{
                    String invName = args[1];
                    if (MXSAttributeInventory.inventoryMap.containsKey(invName)){
                        Player player = (Player) sender;
                        FileUtils.openInventory(player,invName);
                        MsgUtils.sendMsg(player,"&f成功打开了背包");
                        return true;
                    }
                    MsgUtils.sendMsg(sender, "&c不存在的背包名称:&e" + invName);
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        List<String> strings = new ArrayList<>();
        if (!sender.hasPermission("server.admin")){
            strings.add("?");
            return strings;
        }
        if (args.length == 1){
           strings.add("open");
           strings.add("reload");
        }
        if (args.length == 2 && args[0].equalsIgnoreCase("open")){
           strings.addAll(MXSAttributeInventory.inventoryMap.keySet());
        }
        return strings;
    }
}
